/**	
 * <br>
 * Copyright 2011 Chuan. All rights reserved.<br>
 * <br>			 
 * Package: com.chuan.mantu.extend <br>
 * FileName: BitmapDownloaderAsyncTask.java <br>
 * <br>
 * @version
 * @author javacyc@qq.com
 * @created 2012-12-25
 * @last Modified 
 * @history
 */

package com.chuan.mantu.extend;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ImageView;
import android.widget.ViewFlipper;

import com.chuan.mantu.R;
import com.chuan.mantu.abstracts.PicGesture;
import com.chuan.mantu.service.GetPicService;
import com.chuan.mantu.utils.Const;

/**
 * {图片异步下载并添加到ViewFlipper中进行显示}
 *  
 *  @author javacyc@qq.com
 *  @created 2012-12-25 下午02:51:22
 *  @lastModified       
 *  @history           
 */

public class PicShowAsyncTask extends AsyncTask<String, String, List<View>> {
	
	private ViewFlipper flipper; 
	
	private String category;
	
	private Context context;
	
	private String picgestrue;
	
	public PicShowAsyncTask(Context context,ViewFlipper flipper,String category,String picgestrue) {
		this.context = context;
		this.flipper = flipper;
		this.category = category;
		this.picgestrue = picgestrue;
	}
	
	@Override
	protected void onPreExecute() {
		
		super.onPreExecute();
	}
	
	@Override
	protected List<View> doInBackground(String... params) {
		return GetPicService.getShowPicList(context, category, picgestrue);
	}

	@Override
	protected void onProgressUpdate(String... values) {
		super.onProgressUpdate(values);
	}
	
	@Override
	protected void onPostExecute(List<View> result) {
		super.onPostExecute(result);
		PicGesture pg = (PicGesture)context;
		if(!result.isEmpty()){
			recycleBitmap(flipper);
			flipper.removeAllViews();
			for(View view : result){
				flipper.addView(view);
			}
		}else{
			//浏览历史
			if(picgestrue.equals(Const.GESTURE_UP)){
				InfoTipAsyncTask infoTipAsyncTask = new InfoTipAsyncTask(context, R.string.info_tip_up);
				infoTipAsyncTask.execute("");
			}
			
			//浏览更新
			if(picgestrue.equals(Const.GESTURE_DOWN)){
				InfoTipAsyncTask infoTipAsyncTask = new InfoTipAsyncTask(context, R.string.info_tip_down);
				infoTipAsyncTask.execute("");
			}
		}
		pg.offProgressBar();
		pg.setPIC_GESTURE_SWITCH(true);
	}

	/**
	 * 
	 *  ｛回收不显示图片的内存｝
	 *  @param flipper
	 *  @author javacyc@qq.com
	 *  @created 2013-3-3 下午2:16:14
	 *  @lastModified       
	 *  @history
	 */
	private void recycleBitmap( ViewFlipper flipper){
	   if(flipper != null) {
         int count =  flipper.getChildCount();
         for(int i=0; i <count; i++ ){
	    	 ImageView img = (ImageView) flipper.getChildAt(i);
	          if(img != null){
	               Drawable drawable = img.getDrawable( );
	               if(drawable != null){
	                    if(drawable instanceof BitmapDrawable){
	                        BitmapDrawable bitmapDrawable = (BitmapDrawable)drawable;
	                        Bitmap bitmap = bitmapDrawable.getBitmap( );
	                        if(bitmap != null){
	                        	bitmap.recycle();
	                        	bitmap = null;
	                        	System.gc();
	                        }
	                    }
	               }
	           }
          }
	    }
	}
}